from flask import Flask, request
from flask_sqlalchemy import SQLAlchemy
from flask_cors import *

from Controller.diary import diary_blue
from Controller.emotion import emotion_blue
from Controller.login import login_blue

import pymysql

# 跨域支持
def after_request(resp):
    resp.headers['Access-Control-Allow-Origin'] = '*'
    return resp

pymysql.install_as_MySQLdb()
app = Flask(__name__)

# app.after_request(after_request)
CORS(app, supports_credentials=True)

# app.config['SQLALCHEMY_DATABASE_URI'] = "mysql://账号:密码@数据库ip地址:端口号/数据库名"
app.config['SQLALCHEMY_DATABASE_URI'] = "mysql://root:Zjl20191120@localhost:3306/ng_db"

# 关闭数据库修改跟踪操作[提高性能]：
# app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True


# 开启输出底层执行的sql语句
app.config['SQLALCHEMY_ECHO'] = True

# 开启数据库的自动提交功能[一般不使用]
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True

app.config['SQLALCHEMY_POOL_SIZE'] = 100
db = SQLAlchemy(app)



@app.route('/')
def hello_word():
    # user = User.query.get(1)
    # print("主键为1的用户的信息是{}".format(user))
    return "hello, 你来到了张集力编写的我在APP的Flask后端"




app.register_blueprint(emotion_blue)
app.register_blueprint(login_blue)
app.register_blueprint(diary_blue)

if __name__ == '__main__':
    # db.drop_all()
    db.create_all()
    app.run(host='0.0.0.0', port=8000, debug=True, ssl_context=('D:\Program Files\JetBrains\PyCharm 2021.3.3\pythonProject\woZaiFlaskProject\server.crt', 'D:\Program Files\JetBrains\PyCharm 2021.3.3\pythonProject\woZaiFlaskProject\server.key'))